package com.jwthhealth_pub.old.healthyscreening.view.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.healthyscreening.view.HealthyscreeningAnswerActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/21.
 */
public class HealthyScreeningAnswerAdapter extends RecyclerView.Adapter {

    public static final int HIGH = 0;
    public static final int MIDDLE = 1;
    public static final int LOW = 2;

    private Context mContext;
    private int grade;
    private float percent;
    private String gradeDescribe;
    private LinearLayout mProgressbarLayout;
    private List<Integer> list;

    public LinearLayout getmProgressbarLayout() {
        return mProgressbarLayout;
    }

    public HealthyScreeningAnswerAdapter(HealthyscreeningAnswerActivity healthyscreeningAnswerActivity, int grade) {
        this.mContext = healthyscreeningAnswerActivity;
        this.grade = grade;
        this.list = new ArrayList<>(20);
        for (int i=0; i<list.size(); i++) {
            list.add(i, -1);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.ITEM_HEAD.ordinal()) {
            return new HeadViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_healthyscreen_answer_head, parent, false));
        } else {
            return new BodyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_healthyscreen_answer_body, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HeadViewHolder headViewHolder = null;
        BodyViewHolder bodyViewHolder = null;
        if (position == 0) {
            if (grade == HIGH){
                gradeDescribe = mContext.getResources().getString(R.string.healthyscreen_answer_describe_high);
                percent = .75f;
            } else if (grade == MIDDLE){
                gradeDescribe = mContext.getResources().getString(R.string.healthyscreen_answer_describe_middle);
                percent = .5f;
            }else {
                percent = .25f;
                gradeDescribe = mContext.getResources().getString(R.string.healthyscreen_answer_describe_low);
            }
            headViewHolder = (HeadViewHolder) holder;
            this.mProgressbarLayout = headViewHolder.progressBarLayout;
        } else {
            bodyViewHolder = (BodyViewHolder) holder;
            switch (position) {
                case 1:
                    bodyViewHolder.title.setText(R.string.healthyscreen_answer_title_one);
                    if (grade == HIGH){
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_high_one);
                    } else if (grade == MIDDLE){
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_middle_one);
                    }else {
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_low_one);
                    }
                    break;
                case 2:
                    bodyViewHolder.title.setText(R.string.healthyscreen_answer_title_two);

                    if (grade == HIGH){
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_high_two);
                    } else if (grade == MIDDLE){
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_middle_two);
                    }else {
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_low_two);
                    }

                    break;
                case 3:
                    bodyViewHolder.title.setText(R.string.healthyscreen_answer_title_thi);

                    if (grade == HIGH){
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_high_thi);
                    } else if (grade == MIDDLE){
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_middle_thi);
                    }else {
                        bodyViewHolder.content.setText(R.string.healthyscreen_answer_content_low_thi);
                    }

                    break;
                default:break;
            }
        }
    }

    public void putScorePos(int width) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        float marginLeft = width * percent;
        params.setMargins((int)marginLeft, 0, 0, 0);
        TextView textView = new TextView(mContext);
        textView.setPadding(0,0,0,7);
        textView.setText(gradeDescribe);
        textView.setTextSize(10);
        textView.setTextColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER);
        textView.setGravity(Gravity.CENTER);
        if (grade == HIGH) {
            textView.setBackground(mContext.getResources().getDrawable(R.mipmap.high_bg));
        } else if (grade == MIDDLE){
            textView.setBackground(mContext.getResources().getDrawable(R.mipmap.middlebg));
        } else {
            textView.setBackground(mContext.getResources().getDrawable(R.mipmap.score_bg));
        }
        textView.setLayoutParams(params);
        mProgressbarLayout.addView(textView);
    }

    @Override
    public int getItemCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE.ITEM_HEAD.ordinal();
        } else {
            return ITEM_TYPE.ITEM_BODY.ordinal();
        }
    }

    class HeadViewHolder extends RecyclerView.ViewHolder{

        private LinearLayout progressBarLayout;

        public HeadViewHolder(View itemView) {
            super(itemView);

            progressBarLayout = (LinearLayout) itemView.findViewById(R.id.healthy_progress_layout);
        }
    }

    class BodyViewHolder extends RecyclerView.ViewHolder{

        TextView title, content;

        public BodyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_tv_title);
            content = (TextView) itemView.findViewById(R.id.item_tv_content);
        }
    }

    private enum ITEM_TYPE {
        ITEM_HEAD, ITEM_BODY
    }
}
